package com.ecomm.util.dbadapter;

import com.ecomm.exception.DBException;
import com.ecomm.util.Config;
import com.ecomm.util.DAO;
import com.ecomm.util.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;

import java.sql.Connection;
import java.sql.DriverManager;


/**
 * User: hou wanchun
 * Date: 2003-7-21
 * Time: 10:15:49
 */
public class P6spyDAO implements DAO
{
    private static Log log = Logger.getLog(JDBCDAO.class);

    private static String url = null;
    private static String username = null;
    private static String password = null;

    static
    {
        Configuration config = Config.subset("database.p6spy");
        url = config.getString("url");
        username = config.getString("username");
        password = config.getString("password", "");
    }

    /**
     * 取得一个数据库连接
     *
     * @return
     * @throws DBException
     */
    public Connection getConnection() throws DBException
    {
        Connection conn = null;
        try
        {
            Class.forName("com.p6spy.engine.spy.P6SpyDriver");
            conn = DriverManager.getConnection(url, username, password);
        }
        catch (Exception e)
        {
            log.error("不能获得一个数据库连接。", e);
            throw new DBException("不能获得一个数据库连接。错误信息是" + e.getMessage());
        }

        return conn;
    }
}
